package com.example.lcode.backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Question90 {

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> re = new ArrayList<>();
        Integer length = nums.length;
        int startIndex = 0;
        Arrays.sort(nums);
        subsetsWithDup(nums, res, re, length, startIndex);
        return res;
    }

    private void subsetsWithDup(int[] nums, List<List<Integer>> res, List<Integer> re, Integer length, int startIndex) {
        if (re.size() <= length) {
            res.add(new ArrayList<>(re));
        } else {
            return;
        }
        for (int i = startIndex; i < nums.length ; i++) {
            if (i - startIndex> 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            re.add(nums[i]);
            subsetsWithDup(nums, res, re, length, i + 1);
            re.remove(re.size() - 1);
        }
    }

    public static void main(String[] args) {
        Question90 question90 = new Question90();
        int []arr = {0};
        List<List<Integer>> lists = question90.subsetsWithDup(arr);
        System.out.println(lists);
    }

}
